package com.qwz.example;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
* @Description: 上下文，用来包含解释器需要的一些全局信息
* @Package com.qwz.example.Context
* @author Qiuwuzhi
* @date 2019/12/27 9:51
* @version V1.0
*/
public class Context {

    private Element preEle;

    public Context(String fileName) throws Exception{
        //通过辅助的xml工具类来获取被解析的xml对应的document对象
        this.document = XmlUtil.getRoot(fileName);
    }

    public Element getNowEle(Element preEle,String elName){
        NodeList childNodes = preEle.getChildNodes();
        for (int i=0;i<childNodes.getLength();i++){
            if (childNodes.item(i) instanceof Element){
                Element element = (Element) childNodes.item(i);

                if (element.getTagName().equals(elName)){
                    return element;
                }
            }
        }
        return null;
    }

    /**
     * 重新初始化上下文
     */
    public void reInit(){
        this.preEle = null;
    }

    private Document document;

    public Element getPreEle() {
        return preEle;
    }

    public void setPreEle(Element preEle) {
        this.preEle = preEle;
    }

    public Document getDocument() {
        return document;
    }

}
